﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace DomExplorer
{
    class DOMImages
    {
        public static Image ProcessSelectedImage(HtmlElement theImage)
        {
            string path = theImage.GetAttribute("src");

            Image img;

            /** website images need to be extracted from a stream **/
            WebRequest wr = WebRequest.Create(path);
            WebResponse wres = wr.GetResponse();
            Stream strm = wres.GetResponseStream();

            /** sometimes (especially with our security filters) images cant be loaded **/
            try
            {
                img = Image.FromStream(strm);
            }
            catch (ArgumentException)
            {
                return null;
            }

            /** close it **/
            strm.Close();
            strm.Dispose();

            return img;
        }

        public static Image GetImageThumbnail(Image img, Size controlDimension)
        {
            int width;
            int height;

            /** if we need to resize the thumbnail, we will, otherwise use original size **/
            if (img.Width > controlDimension.Width || img.Height > controlDimension.Height)
            {
                width = img.Width / 2;
                height = img.Height / 2;
            }
            else
            {
                width = img.Width;
                height = img.Height;
            }


            /** some images come with thums embeded. this'll get it **/
            return img.GetThumbnailImage(width, height, null, IntPtr.Zero);

        }
    }
}
